home *** CD-ROM | disk | FTP | other *** search
- // sendToPort.c
- //
- // Copyright © 1994 by Rainbow Hill Pty Ltd. All rights reserved.
- //
-
- #include <Errors.h>
- #include <Serial.h>
- #include <Files.h>
- #include <stdio.h>
- #include <console.h>
- #include <string.h>
-
- extern pascal OSErr RAMSDOpen(SPortSel whichPort);
- extern pascal void RAMSDClose(SPortSel whichPort);
-
- static OSErr SetOn(void);
-
- static ioParam outParam;
- static char outBuff[256];
- static SPortSel portID;
- static short refNum;
-
- main() {
- unsigned char title[] = "SendToPort";
- OSErr osErr;
- short kIn;
- short kOut;
- char c;
-
- // set up the console window
- console_options.procID = rDocProc;
- console_options.nrows = 8;
- console_options.ncols = 70;
- CtoPstr(title);
- console_options.title = title;
-
- printf("SendToPort reads strings from the keyboard and sends them to a\n");
- printf("serial port. Note that SendToPort does NOT send a CR at the end\n");
- printf("of each line. To send a control character, prepend a circumflex\n");
- printf("accent: ^C for CNTL-C, ^I for TAB, ^M for CR, ^Z for EOF (a ^\n");
- printf("\"folds\" all ASCII characters onto the first 31). Type ^^ to send\n");
- printf("the circumflex accent itself.\n");
- printf("\n");
-
- // get the port ID
- do {
- printf("Please identify the port (a, A, b, or B) >");
- gets(outBuff);
- if (*outBuff == 'a' || *outBuff == 'A') {
- portID = sPortA;
- refNum = aoutRefNum;
- }
- else if (*outBuff == 'b' || *outBuff == 'B') {
- portID = sPortB;
- refNum = boutRefNum;
- }
- else {
- printf("\nSorry, only 'A' and 'B' are valid port identifiers\n");
- *outBuff = '\0';
- }
- } while (*outBuff == '\0');
-
- // keep asking for new strings unless there is an error
- printf(">");
- do {
- (void)gets(outBuff);
-
- // ignore empty strings
- if (*outBuff != '\0') {
-
- // convert the control characters with a "look backward" algorithm
- kOut = 1;
- kIn = 1;
- while (outBuff[kIn] != '\0') {
- c = outBuff[kIn];
- if (outBuff[kIn - 1] == '^') {
- kOut--;
- if (c != '^') { // with ^^, the second '^' overwrites the first one
- if (c == '!')
- c = 0x7F; // DEL
- else
- c &= 0x1F; // CNTL character
- }
- }
- outBuff[kOut] = c; // possible because kOut never exceeds kIn
- kIn++;
- kOut++;
- }
- outBuff[kOut] = '\0';
-
- // set up the port
- osErr = SetOn();
- if (osErr == noErr) {
-
- // send the string to the port
- outParam.ioReqCount = strlen((const char *)outBuff);
- osErr = PBWrite(&outParam, true);
- if (osErr != noErr) printf("PBWrite error: %d\n", osErr);
-
- // close the port without KillIO(refNum) so that the string goes out
- RAMSDClose(portID);
- }
-
- // prompt for a new string
- printf(">");
-
- } // the string was not empty
- } while (osErr == noErr);
- } // main
-
- //------------------------------------------------------------------------------------------ SetOn
- static OSErr SetOn(void) {
- OSErr osErr;
- SerShk handshake;
-
- // set up the parameter block for transmission
- outParam.ioCompletion = nil;
- outParam.ioBuffer = (Ptr)outBuff;
- outParam.ioPosMode = fsAtMark;
- outParam.ioRefNum = refNum;
-
- // open the modem port for I/O
- osErr = RAMSDOpen(portID);
- if (osErr != noErr) {
- printf("RAMSDOpen error: %d\n", osErr);
- }
- else {
-
- // configure the port
- handshake.fCTS = 1;
- osErr = SerReset(refNum, baud9600 | noParity | data8 | stop10);
- if (osErr != noErr) {
- RAMSDClose(portID);
- printf("SerReset error: %d\n", osErr);
- }
- else {
- osErr = SerHShake(refNum, &handshake);
- if (osErr != noErr) {
- RAMSDClose(portID);
- printf("SerHShake error: %d\n", osErr);
- }
- }
- }
-
- return (osErr);
- } // SetOn
-